package com.stylefeng.guns.rest.modular.attent.controller;


import com.stylefeng.guns.rest.common.CommonResponse;
import com.stylefeng.guns.rest.common.exception.BizExceptionEnum;
import com.stylefeng.guns.rest.common.exception.BussinessException;
import com.stylefeng.guns.rest.common.persistence.model.Article;
import com.stylefeng.guns.rest.common.persistence.model.User;
import com.stylefeng.guns.rest.modular.artitle.controller.dto.ArticleHotResponse;
import com.stylefeng.guns.rest.modular.artitle.service.ArticleService;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentHotRequest;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentUserRequest;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentUserResponse;
import com.stylefeng.guns.rest.modular.attent.service.AttentService;
import com.stylefeng.guns.rest.modular.attent.service.AttentUserService;
import com.stylefeng.guns.rest.modular.users.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ASUS-PC on 2018/6/25.
 */
@RestController
@RequestMapping("/attent")
public class AttentController {
    @Autowired
    AttentService attentService;
    @Autowired
    UserService userService;
    @Autowired
    ArticleService articleService;
    @Autowired
    AttentUserService attentUserService;



    //关注某个用户或取消关注
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public ResponseEntity<?> attentUser(@RequestBody AttentUserRequest request) {
        if(request.getUserId()==0 ||
                request.getAttentedUserId()==0 ||
                request.getType()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getType()==1){//添加关注
            boolean state = attentService.inserAttent(request);
            if(state){
                return ResponseEntity.ok(CommonResponse.ok(null));
            }else {
                throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
            }
        }else {//取消关注
            boolean state = attentService.deleteAttent(request);
            if(state){
                return ResponseEntity.ok(CommonResponse.ok(null));
            }else {
                throw new BussinessException(BizExceptionEnum.UNKNOWN_ERROR);
            }
        }
    }

    //获取热门关注列表
    @RequestMapping(value = "/hotAttent", method = RequestMethod.POST)
    public ResponseEntity<?> hotAttentList(@RequestBody AttentHotRequest request){
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        //获取关注度靠前的用户
        List<User> users = userService.findListByCarenum(request.getPage(),request.getSize());
        List<ArticleHotResponse> hotList = new ArrayList<>();
        if(users!=null && users.size()>0){
            for (int i = 0; i < users.size(); i++) {
                ArticleHotResponse articles = new ArticleHotResponse();
                articles.setNickName(users.get(i).getNickName());
                articles.setAvatar(users.get(i).getAvatar());
                //是否已关注
                if(request.getUserId()>0){
                    int isAttent = attentService.queryAttntByUser(request.getUserId(),users.get(i).getId());
                    if(isAttent==1){
                        articles.setIsAttent(1);
                    }
                }
                //获取用户最新的5条数据
                List<Article> a = articleService.findHotUserArticles(users.get(i).getId());
                if(a!=null && a.size()>0){
                    articles.setArticles(a);
                    hotList.add(articles);
                }else {
                    continue;
                }
            }
        }else {
            return ResponseEntity.ok(CommonResponse.ok(null));
        }
        return ResponseEntity.ok(CommonResponse.ok(hotList));
    }


    //获取我的关注列表
    @RequestMapping(value = "/getUserAttentList", method = RequestMethod.POST)
    public ResponseEntity<?> getUserAttentList(@RequestBody AttentHotRequest request){
        if(request.getUserId()==0){
            throw new BussinessException(BizExceptionEnum.NEED_PARAMS);
        }
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        List<AttentUserResponse> lists = attentUserService.getAttentUserList(request);
        return ResponseEntity.ok(CommonResponse.ok(lists));
    }


    //获取热门关注列表，
    @RequestMapping(value = "/hotAttentList", method = RequestMethod.POST)
    public ResponseEntity<?> hotAttentNew(@RequestBody AttentHotRequest request){
        if(request.getPage()==0){
            request.setPage(1);
        }
        if(request.getSize()==0){
            request.setSize(20);
        }
        if(request.getUserId()>0){
            List<AttentUserResponse> lists = attentUserService.getAttentUserList(request);
            if(lists!=null && lists.size()>0){

            }
        }else{
            //获取关注度靠前的用户
            List<User> users = userService.findListByCarenum(1,20);
            if(users!=null && users.size()>0){

            }
        }



        return ResponseEntity.ok(CommonResponse.ok(null));
    }

}
